package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.FileInfoDto;
import com.project.nimsen.entity.User;
import com.project.nimsen.service.IFileinfoService;
import com.project.nimsen.util.WebUtil;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
import com.project.jdbc.utils.Page;
@Repository("fileinfoService")
public class FileinfoServiceImpl extends BaseServiceImpl implements IFileinfoService {
	@Resource
	IBaseDao baseDao;
	@Resource
	WebUtil webUtil;
	public IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	@Override
	public List<Map<String, Object>> findByDto(FileInfoDto dto)
			throws Exception {
		//当前登录人
		User use = webUtil.getCurrUser();
		Finder finder = new Finder("select id,filename,srcname,autoname,filetype,"
			+ "createTime from fileinfo where active!=1"
			+ " and createUser = :currUserid");
		finder.setParam("currUserid", use.getId());
		return baseDao.queryForList(finder);
	}
	public List<Map<String, Object>> findFileInfoByDto(FileInfoDto dto)
			throws Exception {
		Finder finder = new Finder("select fil.id,fil.filename,fil.srcname,fil.autoname,"
			+ "fil.filetype,fil.createTime from fileinfo as fil left join archivefile as "
			+ "arfi on fil.id = arfi.fileid left join filearchivrelation as fcr on "
			+ "arfi.relationid=fcr.id where fil.active!=1 and arfi.active!=1 and "
			+ "fcr.active!=1");
		if(StringUtils.isNotBlank(dto.getArchid())){
			finder.append(" and arfi.archivesid=:archivesid");
			finder.setParam("archivesid", dto.getArchid());
		}
		if(StringUtils.isNotBlank(dto.getRelatiovalue())){
			finder.append(" and fcr.relationvalue=:relatiovalue");
			finder.setParam("relatiovalue", dto.getRelatiovalue());
		}
		return baseDao.queryForList(finder);
	}
	public List<Map<String, Object>> findTagAnnexbyDocid(FileInfoDto dto)
			throws Exception {
		Finder finder = new Finder("select fif.filename,fif.srcname,fif.filetype,fif.createTime"
			+ ",fcr.relationame,arf.descs from archivefile as arf left join filearchivrelation "
			+ "as fcr on arf.relationid=fcr.id left join fileinfo as fif on arf.fileid=fif.id "
			+ "where arf.active!=1 and fcr.active!=1 and fif.active!=1 and "
			+ "arf.archivesid=:archivesid and fcr.relationvalue=:relationvalue");
		finder.setParam("archivesid", dto.getArchid());
		finder.setParam("relationvalue", dto.getRelatiovalue());
		finder.append(" order by fif.createTime ").append(dto.getOrderType());
		//当查询全部类型的附件时
		if(dto.getNum()<1)
			return baseDao.queryForList(finder);
		Page page = new Page(1);
		page.setPageSize(dto.getNum());
		return baseDao.queryForList(finder, page);
	}
	public List<Map<String, Object>> findImgListByDto(FileInfoDto dto)
			throws Exception {
		Finder finder = new Finder("SELECT arf.id,arf.archivesid,arf.fileid,arf.descs,arf.relationid"
			+ ",arf.createUser,arf.createTime,arf.active,fif.filename FROM archivefile as arf left "
			+ "join fileinfo as fif on arf.fileid=fif.id left join filearchivrelation as fcr on "
			+ "arf.relationid=fcr.id  where fif.active!=1 and arf.active!=1 and fcr.active!=1");
		if(StringUtils.isNotBlank(dto.getArchid())){
			finder.append(" and arf.archivesid=:archivesid");
			finder.setParam("archivesid", dto.getArchid());
		}
		if(StringUtils.isNotBlank(dto.getRelatiovalue())){
			finder.append(" and fcr.relationvalue=:relatiovalue");
			finder.setParam("relatiovalue", dto.getRelatiovalue());
		}
		return baseDao.queryForList(finder);
	}

}
